<template>
    <div>
        <a-table :dataSource="taskList"
                 :columns="columns"
                 :pagination="false"
                 style="margin-top: 10px"
                 bordered>
            <template #createTime="{text,record}">
                {{this.$moment(text).format('YYYY-MM-DD HH:mm:ss')}}
            </template>
            <template v-slot:action="{text,record}">
                <a-space>
                    <a-button type="primary" @click="showDrawer(record)">办理任务</a-button>
                    <a-popconfirm
                            title="删除数据无法恢复，请确认是否删除？"
                            ok-text="是"
                            cancel-text="否"
                            @confirm="okDelete(record.id)"
                    >
                        <a-button type="danger">删除</a-button>
                    </a-popconfirm>
                </a-space>
            </template>
        </a-table>
        <a-drawer
                :title="task.name"
                :width="1000"
                :visible="visible"
                :body-style="{ paddingBottom: '40px' }"
                :footer-style="{ textAlign: 'right' }"
                @close="onClose"
        >
            <a-form layout="vertical" :model="drawerData.workOrderInfo">
                <a-row :gutter="16">
                    <a-col :span="8">
                        <a-form-item
                                label="废品类型"
                                name="wasteTypeId">
                            <a-input v-model:value="drawerData.workOrderInfo.detailVo.wasteTypeName"
                                     :disabled="true"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-item
                                label="废品"
                                name="wasteId">
                            <a-input v-model:value="drawerData.workOrderInfo.detailVo.wasteName"
                                     :disabled="true"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-item
                                label="重量(吨)"
                                name="weight">
                            <a-input v-model:value="drawerData.workOrderInfo.detailVo.weight"
                                     :disabled="true"></a-input>
                        </a-form-item>
                    </a-col>
                </a-row>
                <a-row :gutter="16">
                    <a-col :span="8">
                        <a-form-item
                                label="主害成份"
                                name="component">
                            <a-input v-model:value="drawerData.workOrderInfo.detailVo.component"
                                     :disabled="true"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-item
                                label="废品形态"
                                name="morphological">
                            <a-input v-model:value="drawerData.workOrderInfo.detailVo.morphological"
                                     :disabled="true"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="8">
                        <a-form-item
                                label="包装方式"
                                name="packaging">
                            <a-input v-model:value="drawerData.workOrderInfo.detailVo.packaging"
                                     :disabled="true"></a-input>
                        </a-form-item>
                    </a-col>
                </a-row>
                <a-row :gutter="16">
                    <a-col :span="12">
                        <a-form-item
                                label="产废方"
                                name="createOfficeName">
                            <a-input v-model:value="drawerData.workOrderInfo.createOfficeName"
                                     :disabled="true"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-item
                                label="联系电话"
                                name="createUserPhone">
                            <a-input v-model:value="drawerData.workOrderInfo.createUserPhone"
                                     :disabled="true"></a-input>
                        </a-form-item>
                    </a-col>
                </a-row>
                <a-row :gutter="16">
                    <a-col :span="12">
                        <a-form-item
                                label="处置方"
                                name="createOfficeName">
                            <a-input v-model:value="drawerData.workOrderInfo.recipientOfficeName"
                                     :disabled="true"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="12">
                        <a-form-item
                                label="联系电话"
                                name="createUserPhone">
                            <a-input v-model:value="drawerData.workOrderInfo.recipientUserPhone"
                                     :disabled="true"></a-input>
                        </a-form-item>
                    </a-col>
                </a-row>
                <a-row :gutter="16">
                    <a-col :span="12">
                        <a-form-item
                                label="运输方"
                                name="createOfficeName">
                            <a-input v-model:value="drawerData.workOrderInfo.transportOfficeName"
                                     :disabled="true"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="6">
                        <a-form-item
                                label="联系电话"
                                name="createUserPhone">
                            <a-input v-model:value="drawerData.workOrderInfo.transportUserPhone"
                                     :disabled="true"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="6">
                        <a-form-item
                                label="车牌号"
                                v-bind="validateInfos.plateNumber"
                                name="createUserPhone">
                            <a-input v-model:value="drawerData.plateNumber"
                                     :disabled="plateNumberDisabled"></a-input>
                        </a-form-item>
                    </a-col>
                </a-row>
                <a-row :gutter="16">
                    <a-col :span="24">
                        <a-form-item label="批注"
                                     v-bind="validateInfos.comment"
                                     name="comment">
                            <a-textarea v-model:value="drawerData.comment" :rows="3"/>
                        </a-form-item>
                    </a-col>
                </a-row>
                <a-row :gutter="16">
                    <a-col :span="24">
                        <a-space>
                            <a-button v-for="(item,index) in drawerData.buttonList"
                                      :key="index" type="primary"
                                      @click="submitOk(item)">
                                {{item}}
                            </a-button>
                        </a-space>
                    </a-col>
                </a-row>
            </a-form>
            <a-table :dataSource="drawerData.comments"
                     :columns="commentColumns"
                     :pagination="false"
                     bordered>
                <template #user="{text,record}">
                    {{beLongUser(text)}}
                </template>
                <template #time="{text,record}">
                    {{this.$moment(text).format('YYYY-MM-DD HH:mm:ss')}}
                </template>
            </a-table>
        </a-drawer>
    </div>
</template>

<script>
    import {ref, onMounted} from 'vue'
    import axios from 'axios'
    import {message, Form} from 'ant-design-vue'
    // import {SyncOutlined, ExclamationCircleOutlined} from '@ant-design/icons-vue'
    import cookies from 'js-cookie'
    export default {
        name: "order-demandBill",
        // components: {
        //     SyncOutlined,
        //     ExclamationCircleOutlined
        // },
        setup() {
            const columns = [
                {
                    title: '任务ID',
                    dataIndex: 'id',
                    key: 'id',
                    align: 'center'
                },
                {
                    title: '任务名称',
                    dataIndex: 'name',
                    key: 'name',
                    align: 'center'
                },
                {
                    title: '申请时间',
                    dataIndex: 'createTime',
                    key: 'createTime',
                    slots: {customRender: 'createTime'},
                    align: 'center'
                },
                {
                    title: '操作',
                    key: 'action',
                    slots: {customRender: 'action'},
                    align: 'center'
                }
            ]
            const commentColumns = [
                {
                    title: '时间',
                    dataIndex: 'time',
                    key: 'time',
                    slots: {customRender: 'time'}
                },
                {
                    title: '批注人',
                    dataIndex: 'userId',
                    key: 'userId',
                    slots: {customRender: 'user'}
                },
                {
                    title: '批注信息',
                    dataIndex: 'fullMessage',
                    key: 'fullMessage',
                },
            ]

            const useForm = Form.useForm
            const loginUser = ref({})
            const task = ref({})
            const taskList = ref()
            const userList = ref([])
            const drawerData = ref({
                comment: ''
            })
            const rulesRef = ref({
                comment: [{required: true, message: '请输入批注信息', trigger: ['submit', 'change']}]
            })
            const {validate, validateInfos} = useForm(drawerData, rulesRef.value)
            const visible = ref(false)
            const disabledFlag = ref(true)
            const plateNumberDisabled = ref(true)

            const load = () => {
                axios.get('detailProcess/queryDetailTaskByUserId/' + loginUser.value.id)
                    .then(rs => {
                        console.log("查找待办事务结果：", rs)
                        taskList.value = rs.data.data
                    })
            }

            const okDelete = (id) => {
                console.log("要删除的ID：" + id)
                axios.delete('/task/delete/' + id).then(rs => {
                    if (rs.data.code == 200) {
                        message.success("删除成功")
                        load()
                    } else {
                        message.error(rs.data.message)
                    }
                })
            }

            const showDrawer = obj => {
                task.value = JSON.parse(JSON.stringify(obj))
                axios.get('/detailProcess/viewTaskForm/' + obj.id).then(rs => {
                    console.log("获取task信息结果：", rs)
                    drawerData.value = rs.data.data
                    drawerData.value.comment = ''
                    drawerData.value.plateNumber = drawerData.value.workOrderInfo.detailVo.plateNumber
                    if (task.value.name == '运输方确认'){
                        plateNumberDisabled.value = false
                        drawerData.value.plateNumber = undefined
                        rulesRef.value.plateNumber = [{required: true, message: '请输入运输车牌号', trigger: ['submit', 'change']}]
                    }
                    visible.value = true
                })
            }
            const onClose = () => {
                console.log("关闭")
                visible.value = false
            }
            const submitOk = (buttonValue) => {
                // message.success("提交")
                validate().then(() => {
                    axios.post('/detailProcess/complete', {
                        plateNumber: !plateNumberDisabled.value ? drawerData.value.plateNumber : undefined,
                        buttonValue: buttonValue,
                        userId: loginUser.value.id,
                        taskId: task.value.id,
                        comment: drawerData.value.comment
                    }).then(rs => {
                        if (rs.data.code == 200) {
                            message.success("审批成功")
                            visible.value = false
                            load()
                        }
                    })
                }).catch(() => {
                    console.log("校验不通过")
                })
            }

            const beLongUser = (userId) => {
                let rs = ''
                userList.value.forEach(val => {
                    if (val.id == userId) {
                        rs = val.name
                        return rs
                    }
                })
                return rs == '' ? userId : rs
            }

            onMounted(() => {
                loginUser.value = JSON.parse(cookies.get("user"))
                axios.get('sys-user/queryAll').then(rs=>{
                    userList.value = rs.data.data
                })
                load()
            })
            return {
                columns,
                load,
                okDelete,
                task,
                taskList,
                showDrawer,
                visible,
                onClose,
                submitOk,
                drawerData,
                commentColumns,
                validateInfos,
                loginUser,
                disabledFlag,
                userList,
                beLongUser,
                plateNumberDisabled
            }
        }
    }
</script>

<style scoped>

</style>